package com.gm.lib.utils;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.gm.common.context.GlobalContext;
import com.gm.common.utils.ResUtil;
import com.gm.lib.R;


public class GMToastUtil {


    public static void showToast(int messageID) {
        showToast(GlobalContext.getContext(), messageID);
    }


    public static void showToast(String message) {
        showToast(GlobalContext.getContext(), message);
    }


    public static void showToast(int messageID, int duration) {
        showToast(GlobalContext.getContext(), ResUtil.getString(messageID), duration);
    }

    public static void showToast(String message, int duration) {
        showToast(GlobalContext.getContext(), message, duration);
    }


    public static void showToast(Context context, int resId) {
        ///Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
        showToast(context, context.getString(resId), Toast.LENGTH_SHORT);
    }

    public static void showToast(Context context, String message) {
        showToast(context, message, Toast.LENGTH_SHORT);
    }

    public static void showToast(Activity context, int resId) {
        showToast(context, context.getString(resId), Toast.LENGTH_SHORT);
    }

    private static void showToast(Activity context, String message) {
        showToast(context, message, Toast.LENGTH_SHORT);
    }


    private static void showToast(Context context, int resId, int duration) {
        ///Toast.makeText(context, resId, duration).show();
        showToast(context, context.getString(resId), duration);
    }

    static Toast toast;
    static TextView contentView;


    private static void showToast(Context context, String message, int duration) {
        if (context == null) {
            return;
        }

        try {
            if (toast == null || contentView == null) {
                int dp30 = GMViewUtil.dip2px(context, 30);
                int dp20 = GMViewUtil.dip2px(context, 20);
                contentView = new TextView(context);
                contentView.setGravity(Gravity.CENTER);
                contentView.setBackgroundResource(R.drawable.bg_toast);
                contentView.setTextSize(16);
                contentView.setTextColor(context.getResources().getColor(R.color.white));
                contentView.setPadding(dp30, dp20, dp30, dp20);

                toast = new Toast(context);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setView(contentView);
                toast.setDuration(duration);

            }

            contentView.setText(message);

            toast.show();
        }catch (Exception e){
            Toast.makeText(context,message,duration).show();
            e.printStackTrace();
        }


    }


}
